#include "SypEngine.h"

#ifdef CHECKMEM
#include "mmgr.h"
#endif
namespace Syp
{

SubMaterial::SubMaterial(): btype(NOBLEND), lit(true)
{
	for(int i=0;i<3;++i)
	{
		diffuse[i] = ambient[i] = specular[i] = 0.0f;
	}
	diffuse[3] = ambient[3] = specular[3] = 1.0f;	
	diffuseMap = 0;
	useEffect("DefaultFF");
}
SubMaterial::SubMaterial(const SubMaterial& sm)
{
	copy(sm);	
}

void SubMaterial::copy(const SubMaterial& sm)
{
	for(int i=0;i<4;++i)
	{
		diffuse[i] = sm.diffuse[i]; 
		ambient[i] = sm.ambient[i];
		specular[i] = sm.specular[i];
	}
	shininess = sm.shininess;
	diffuseMap = sm.diffuseMap;
	btype = sm.btype;
	lit = sm.lit;
	effect = sm.effect;
	if(diffuseMap)
	{
	diffuseMap->addInstance();
	}
}

SubMaterial& SubMaterial::operator =(const SubMaterial& sm)
{	
    copy(sm);
	return *this;
}
SubMaterial::~SubMaterial()
{
	clearTexture();
}

Material::Material(const String& filename):Resource(filename)
{	
	
}

void SubMaterial::useTexture(const String& filename)
{
	if(filename == "")
	{
		return;
	}
	useTexture(TextureObj::loadTexture(filename,true,true,true));
	
}

void SubMaterial::useTexture(TextureObj* txtID)
{
	clearTexture();
	if(txtID)
	{
	//Increment reference count
	txtID->addInstance();
    diffuseMap = txtID;
	}
}

Effect* SubMaterial::getEffect()
{
	return effect;
}

void SubMaterial::useEffect(const String& e)
{
	Effect* ef = EffectRegistry::getRegistry().getEntry(e);
	assert(ef && "Effect not found.");
	if(ef)
	{
		useEffect(ef);
	}

}

void SubMaterial::useEffect(Effect* e)
{
	effect = e;
}

Material* Material::createMaterial(const String& filename)
{
	return (new Material(filename));
}

void Material::destroyMaterial(Material* m)
{
	delete m;
}

void SubMaterial::clearTexture()
{
	if(diffuseMap)
	{
		diffuseMap->removeInstance();
	}
	diffuseMap = 0;
}

TextureObj* SubMaterial::getTexture()
{
	return diffuseMap;
}

Material* Material::loadFromFile(const String& fileName)
{
	std::cout<<"Loading "<<fileName<<"\n";
	Material* mat = static_cast<Material*>(ResourceManager::getResourceManager().isResourceLoaded(fileName));
	if(mat)
	{
		return mat;
	}
	mat = new Material(fileName);
	String temp;
	InputStream fin;
	fin.open(fileName.c_str());
	assert(fin.is_open() && "Material file could not be found.");
	//Read Header
	fin>>temp;
	//Get Material Filename
	getline(fin,temp);
	
	///Read in DiffuseMap Header
		fin>>temp;
	


	while(!fin.eof() && temp!="End")
	{
		SubMaterial sm;

		
		fin>>temp;
		if(temp != "0")
		{
			sm.useTexture(temp);
		}
		//Read Diffuse Header
		fin>>temp;
		//Read in values
		for(int i=0;i<3;++i)
		{
			fin>>sm.diffuse[i];
		}

		//Read Ambient Header
		fin>>temp;
		//Read in values
		for(int i=0;i<3;++i)
		{
			fin>>sm.ambient[i];
		}

		//Read Specular Header
		fin>>temp;
		//Read in values
		for(int i=0;i<3;++i)
		{
			fin>>sm.specular[i];
		}

		//Shininess
		fin>>temp;
		fin>>sm.shininess;
		//Effect
		fin>>temp;
		fin>>temp;
		sm.useEffect(temp);



		mat->subMaterials.push_back(sm);	
		//Either End or MaterialName
		fin>>temp;
	}

	fin.close();
    return mat;
}

}
